using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace SQLString.AppResource
{
	class Monitor
	{
		FileInfo logFILE = null;
		FileStream logFilestr = null;
		StreamWriter srLogWR = null;
		private static string szCap = "...Application";

		#region Common Error Message Dialogs
		public static void sErrMsg( string Msg )
		{
			MessageBox.Show("Application Error: " + Msg, szCap, MessageBoxButtons.OK, MessageBoxIcon.Error);
		}
		public static void sMsg( string Msg, MessageBoxIcon mbIcon )
		{
			MessageBox.Show(Msg, szCap, MessageBoxButtons.OK, mbIcon);
		}
		public static void sPrintErrMsg( string Msg )
		{
			MessageBox.Show("Print Error: " + Msg, szCap, MessageBoxButtons.OK, MessageBoxIcon.Error);
		}
		#endregion

		public Monitor(String szFileName)
		{
			if (OpenMonitor(szFileName) != true)
			{
				sMsg("System Logging is not enabled for the file identified.  Monitor defaulting to standard log file.",MessageBoxIcon.Warning);
				if (OpenMonitor(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).ToString() + "\\" 
					+ Application.ProductName.ToString().Trim() + "_LOG.LOG", FileMode.CreateNew) != true)
				{
					sMsg("System Logging is not enabled for Monitor Default Configuration.  TERMINATING.", MessageBoxIcon.Warning);
					this.MyStatus = false;
					return;
				}
				else
					this.MyStatus = true;
			}
			else
				this.MyStatus = true;
		}

		private Boolean bStatus = false;
		public Boolean MyStatus
		{
			get
			{
				return bStatus;
			}
			set{bStatus=value;}
		}
		private bool OpenMonitor(String szFileName)
		{
			try
			{
				if (szFileName != null && szFileName.Length > 0)
				{
					logFILE = new FileInfo(szFileName);
					if (logFILE.Exists)
					{
						logFilestr = new FileStream(logFILE.FullName, FileMode.Append);
						if (logFilestr.CanWrite)
						{
							srLogWR = new StreamWriter(logFilestr);
							return true;
						}
						else
							return false;
					}
					else
						return false;
				}
				else
					return false;
			}
			catch (FileNotFoundException ex)
			{
				sErrMsg(ex.Message);
				return false;
			}
			catch (System.IO.EndOfStreamException ex)
			{
				sErrMsg(ex.Message);
				return false;
			}
			catch (UnauthorizedAccessException ex)
			{
				sErrMsg(ex.Message);
				return false;
			}
			catch (IOException ex)
			{
				sErrMsg(ex.Message);
				return false;
			}
			catch (Exception ex)
			{
				sErrMsg(ex.Message);
				return false;
			}
		}

		private bool OpenMonitor( String szFileName, FileMode fmOPEN )
		{
			try
			{
				if (szFileName != null && szFileName.Length > 0)
				{
					logFILE = new FileInfo(szFileName);
					if (!logFILE.Exists)
					{
						logFilestr = new FileStream(logFILE.FullName, fmOPEN);
						srLogWR = new StreamWriter(logFilestr);
						return true;
					}
					else if (logFILE.Exists)
					{
						fmOPEN = FileMode.Append;
						logFilestr = new FileStream(logFILE.FullName, fmOPEN);
						srLogWR = new StreamWriter(logFilestr);
						return true;
					}
					else
						return false;
				}
				else
					return false;
			}
			catch (FileNotFoundException ex)
			{
				sErrMsg(ex.Message);
				return false;
			}
			catch (System.IO.EndOfStreamException ex)
			{
				sErrMsg(ex.Message);
				return false;
			}
			catch (UnauthorizedAccessException ex)
			{
				sErrMsg(ex.Message);
				return false;
			}
			catch (IOException ex)
			{
				sErrMsg(ex.Message);
				return false;
			}
			catch (Exception ex)
			{
				sErrMsg(ex.Message);
				return false;
			}
		}

		public bool CloseLog()
		{
			try
			{
				if (srLogWR != null && logFilestr!=null)		//	streams and fileinfo object still open
				{
					srLogWR.Flush();
					logFilestr.Flush();
					logFilestr.Dispose();
					//srLogWR.Dispose();
					return true;
				}
				else
					return false;
			}
			catch (FileNotFoundException ex)
			{
				sErrMsg(ex.Message);
				return false;
			}
			catch (System.IO.EndOfStreamException ex)
			{
				sErrMsg(ex.Message);
				return false;
			}
			catch (UnauthorizedAccessException ex)
			{
				sErrMsg(ex.Message);
				return false;
			}
			catch (IOException ex)
			{
				sErrMsg(ex.Message);
				return false;
			}
			catch (Exception ex)
			{
				sErrMsg(ex.Message);
				return false;
			}
		}

		public bool WriteLogMsg( String szMsg )
		{

			try
			{
				if (szMsg != null && szMsg.Length > 0)	//	message is not void
				{
					if (srLogWR != null)		//	stream is open
					{
						srLogWR.WriteLine(GetTimeStamp(1) + "\t\tEVENT MESSAGE:\t" + szMsg);
						return true;
					}
					else
						return false;
				}else
					return false;
			}
			catch (FileNotFoundException ex)
			{
				sErrMsg(ex.Message);
				return false;
			}
			catch (System.IO.EndOfStreamException ex)
			{
				sErrMsg(ex.Message);
				return false;
			}
			catch (UnauthorizedAccessException ex)
			{
				sErrMsg(ex.Message);
				return false;
			}
			catch (IOException ex)
			{
				sErrMsg(ex.Message);
				return false;
			}
			catch (Exception ex)
			{
				sErrMsg(ex.Message);
				return false;
			}
		}

		public bool WriteLogMsg( String szMsg, int iType )
		{

			try
			{
				if (srLogWR != null)		//	stream is open
				{
					if (iType == 1)
					{
						srLogWR.WriteLine(GetTimeStamp(1) + "\t\tEVENT MESSAGE:\t" + szMsg);
					}
					else if (iType == 2)//	write buffer spacing messages
					{
						srLogWR.WriteLine("\r");
					}
					return true;
				}
				else
					return false;
			}
			catch (FileNotFoundException ex)
			{
				sErrMsg(ex.Message);
				return false;
			}
			catch (System.IO.EndOfStreamException ex)
			{
				sErrMsg(ex.Message);
				return false;
			}
			catch (UnauthorizedAccessException ex)
			{
				sErrMsg(ex.Message);
				return false;
			}
			catch (IOException ex)
			{
				sErrMsg(ex.Message);
				return false;
			}
			catch (Exception ex)
			{
				sErrMsg(ex.Message);
				return false;
			}
		}

		public bool WriteERRORLogMsg( String MethodName, String szErrorMessage, Exception inner)
		{

			try
			{
				if (MethodName != null && MethodName.Length > 0)	//	message is not void
				{
					if (srLogWR != null)		//	stream is open
					{
						if (inner != null)
						{
							srLogWR.WriteLine(GetTimeStamp(1) + "\t\tEXCEPTION IN:\t" + MethodName + "\tOutter:\t" + szErrorMessage.Trim() + "\n\t\tINNER: " + inner.ToString().Trim());
						}
						else
						{
							srLogWR.WriteLine(GetTimeStamp(1) + "\t\tEXCEPTION IN:\t" + MethodName + "\tOutter:\t" + szErrorMessage.Trim() + "\n\t\tINNER: was null" );
						}
						return true;
					}
					else
						return false;
				}
				else
					return false;
			}
			catch (FileNotFoundException ex)
			{
				sErrMsg(ex.Message);
				return false;
			}
			catch (System.IO.EndOfStreamException ex)
			{
				sErrMsg(ex.Message);
				return false;
			}
			catch (UnauthorizedAccessException ex)
			{
				sErrMsg(ex.Message);
				return false;
			}
			catch (IOException ex)
			{
				sErrMsg(ex.Message);
				return false;
			}
			catch (Exception ex)
			{
				sErrMsg(ex.Message);
				return false;
			}
		}

		private string GetTimeStamp(int iYearOpt)
		{
			DateTime VTRS = DateTime.Now;
			string TRS = "LOG: " + VTRS.Day.ToString() + "." + VTRS.Month.ToString() + "." + VTRS.Year.ToString();
			if (iYearOpt == 1)
			{
				TRS += " - " + VTRS.Hour + "." + VTRS.Minute.ToString() + "." + VTRS.Second.ToString() + "." + VTRS.Millisecond.ToString();
				return TRS.Trim();
			}
			else
				return TRS.Trim();
		}
	}
}
